﻿namespace ProgrammingEducationSystem.Tools
{
    //字符串时间格式处理
    public static class ClassRoomTimeTools
    {

        /// <summary>
        /// 将时间转换为规定的字符串格式
        /// 如何该时间段不在上课时间内则返回空值
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string? ToStr(DateTime date)
        {
            return ToStr(date, GetHNumByHour(date.Hour) ?? 0);
        }

        /// <summary>
        /// 将时间转换为规定的字符串格式
        /// 如何该时间段不在上课时间内则返回空值
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string? ToStr(DateTime date, int hNum)
        {
            var year = date.Year;
            var month = date.Month;
            var day = date.Day;
            if (hNum == 0) return null;
            return $"{year}-{month}-{day}-{hNum}";
        }

        /// <summary>
        /// 根据小时判断当前在第几节课，不在课程时间则为null
        /// </summary>
        /// <param name="hour"></param>
        /// <returns></returns>
        public static int? GetHNumByHour(int hour)
        {
            switch (hour)
            {
                case 8:
                    return 1;
                case 10:
                    return 2;
                case 14:
                    return 3;
                case 16:
                    return 4;
                default:
                    return null;
            }
        }

        public static int? GetHourByHNum(int hnum)
        {
            switch (hnum)
            {
                case 1:
                    return 8;
                case 2:
                    return 10;
                case 3:
                    return 14;
                case 4:
                    return 16;
                default:
                    return null;
            }
        }

        //时间格式不正确则返回null
        /// <summary>
        /// 获取下课时间
        /// </summary>
        /// <param name="timeStr"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime(string timeStr)
        {
            try
            {
                var timeInfo = timeStr.Split("-");
                DateTime date = new DateTime(
                    year: int.Parse(timeInfo[0]),
                    month: int.Parse(timeInfo[1]),
                    day: int.Parse(timeInfo[3]),
                    hour: GetHourByHNum(int.Parse(timeInfo[4])) + 1 ?? 0,
                    minute: 0,
                    second: 0
                    );
                return date;
            }
            catch (Exception err)
            {
                Console.WriteLine(err);
                Console.WriteLine("-------时间格式不正确--------");
                Console.WriteLine(timeStr);
                return null;
            }
        }


        /// <summary>
        /// 判断某节课是否已经过了下课时间
        /// </summary>
        /// <param name="timeStr"></param>
        /// <returns></returns>
        public static bool IsExpired(string timeStr)
        {
            var date = ToDateTime(timeStr);
            if (DateTime.Now > date) return true;
            return false;
        }

        /// <summary>
        /// 获取当前正在上课的时间字符串
        /// </summary>
        /// <returns></returns>
        public static string? NowCourseHourTime()
        {
            DateTime time = DateTime.UtcNow;
            return ToStr(time);
        }

        /// <summary>
        /// 判断是否正在上课
        /// </summary>
        /// <returns></returns>
        public static bool InClass(string timeStr)
        {
            return timeStr == NowCourseHourTime();
        }
        /// <summary>
        /// 判断是否还未上课
        /// </summary>
        /// <param name="timeStr"></param>
        /// <returns></returns>
        public static bool NotExpired(string timeStr)
        {
            return ToDateTime(timeStr) > ToDateTime(NowCourseHourTime());
        }

    }
}
